import { _decorator, Component, Node, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    
    onLoad () {
        this._initData();
        this._initUI();
        this._initWebsocket();
        this._initDeviceMotion();
    }

    onDeviceMotionEvent (event) {
        let motionValue = jsb.Device.getDeviceMotionValue();
        if (this._websocket.readyState === WebSocket.OPEN) {
            this._websocket.send(
                JSON.stringify(
                    {
                        "type"              : "motion",
                        "accX"              : event.acc.x,
                        "accY"              : event.acc.y,
                        "accZ"              : event.acc.z,
                        "accelerationX"     : motionValue[0],
                        "accelerationY"     : motionValue[1],
                        "accelerationZ"     : motionValue[2],
                        "gravityX"          : motionValue[3],
                        "gravityY"          : motionValue[4],
                        "gravityZ"          : motionValue[5],
                        "rotationRateAlpha" : motionValue[6],
                        "rotationRateBeta"  : motionValue[7],
                        "rotationRateGamma" : motionValue[8],
                        "pitch"             : motionValue[9],
                        "roll"              : motionValue[10],
                        "yaw"               : motionValue[11],
                        "quaternionX"       : motionValue[12],
                        "quaternionY"       : motionValue[13],
                        "quaternionZ"       : motionValue[14],
                        "quaternionW"       : motionValue[15],
                    }
                )
            );
        }

        let x = parseFloat(event.acc.x).toFixed(5);
        let y = parseFloat(event.acc.y).toFixed(5);
        let z = parseFloat(event.acc.z).toFixed(5);
        //
        this.node.getChildByName("eventAcceleration").getChildByName("accXValue").getComponent(cc.LabelComponent).string = x;
        this.node.getChildByName("eventAcceleration").getChildByName("accYValue").getComponent(cc.LabelComponent).string = y;
        this.node.getChildByName("eventAcceleration").getChildByName("accZValue").getComponent(cc.LabelComponent).string = z;
        
        this.node.getChildByName("acceleration").getChildByName("accelerationXValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[0]).toFixed(5);
        this.node.getChildByName("acceleration").getChildByName("accelerationYValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[1]).toFixed(5);
        this.node.getChildByName("acceleration").getChildByName("accelerationZValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[2]).toFixed(5);

        this.node.getChildByName("gravity").getChildByName("gravityXValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[3]).toFixed(5);
        this.node.getChildByName("gravity").getChildByName("gravityYValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[4]).toFixed(5);
        this.node.getChildByName("gravity").getChildByName("gravityZValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[5]).toFixed(5);

        this.node.getChildByName("rotationRate").getChildByName("rotationRateAlphaValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[6]).toFixed(5);
        this.node.getChildByName("rotationRate").getChildByName("rotationRateBetaValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[7]).toFixed(5);
        this.node.getChildByName("rotationRate").getChildByName("rotationRateGammaValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[8]).toFixed(5);

        this.node.getChildByName("attitude").getChildByName("pitchValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[9]).toFixed(5);
        this.node.getChildByName("attitude").getChildByName("rollValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[10]).toFixed(5);
        this.node.getChildByName("attitude").getChildByName("yawValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[11]).toFixed(5);

        this.node.getChildByName("quaternion").getChildByName("quaternionXValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[12]).toFixed(5);
        this.node.getChildByName("quaternion").getChildByName("quaternionYValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[13]).toFixed(5);
        this.node.getChildByName("quaternion").getChildByName("quaternionZValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[14]).toFixed(5);
        this.node.getChildByName("quaternion").getChildByName("quaternionWValue").getComponent(cc.LabelComponent).string = parseFloat(motionValue[15]).toFixed(5);
    }

    _initData() {
        this._websocket = null;
    }

    _initUI() {
        this.node.getChildByName("connection").getChildByName("DeviceStateValue").getComponent(cc.LabelComponent).string = "Disconnected";
        this.node.getChildByName("connection").getChildByName("ControllerStateValue").getComponent(cc.LabelComponent).string = "Disconnected";
    }

    _initDeviceMotion() {
        cc.systemEvent.setAccelerometerEnabled(true);
        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    }

    _initWebsocket() {
        this._websocket = new WebSocket("ws://192.168.50.152:5000?role=controller");
        this._websocket.onopen = this._onopen.bind(this);
        this._websocket.onmessage = this._onmessage.bind(this);
        this._websocket.onerror = this._onerror.bind(this);
        this._websocket.onclose = this._onclose.bind(this);
    }

    _onopen(event) {
        this.node.getChildByName("connection").getChildByName("ControllerStateValue").getComponent(cc.LabelComponent).string = "Connected";
    }

    _onmessage(event) {
        let data = JSON.parse(event.data);
        if (data['event'] == 'connected') {
            this.node.getChildByName("connection").getChildByName("DeviceStateValue").getComponent(cc.LabelComponent).string = "Connected";
        }
        else if (data['event'] == 'disconnected') {
            this.node.getChildByName("connection").getChildByName("DeviceStateValue").getComponent(cc.LabelComponent).string = "Disconnected";
        }
    }

    _onerror(event) {
        console.log("onerror: " + event.data);
        this.node.getChildByName("connection").getChildByName("ControllerStateValue").getComponent(cc.LabelComponent).string = "Disconnected";
    }

    _onclose(event) {
        this.node.getChildByName("connection").getChildByName("ControllerStateValue").getComponent(cc.LabelComponent).string = "Disconnected";
    }

}
